<?php
namespace app\common\model;

use think\Model;

class Drive extends Model
{
    //平台驾驶员列表
    public function platformdrive($page,$limit,$where){
       $list = db('drive')
           ->alias('d')
           ->join('company c',"c.company_id = d.company_id","left")
           ->join('store s',"s.store_id = d.store_id",'left')
           ->where($where)
           ->page($page,$limit)
           ->select();
        $count = db('drive')
            ->alias('d')
            ->join('company c',"c.company_id = d.company_id","left")
            ->join('store s',"s.store_id = d.store_id",'left')
            ->where($where)
            ->count();
        return ['count' => $count,'data' => $list];
    }
    //临时驾驶员列表
   public function temporarydrive($page,$limit,$where){
       $list = db('drive')
           ->alias('d')
           ->join('company c',"c.company_id = d.company_id","left")
           ->join('store s',"s.store_id = d.store_id",'left')
           ->where($where)
           ->page($page,$limit)
           ->select();
       foreach ($list as $k=>$v){
           $iscar = db('car')->where('drive_id',$list[$k]['drive_id'])->count();
           if ($iscar){
               $list[$k]['ishavecar'] = 1;
           }else{
               $list[$k]['ishavecar'] = 2;
           }
       }
       $count = db('drive')
           ->alias('d')
           ->join('company c',"c.company_id = d.company_id","left")
           ->join('store s',"s.store_id = d.store_id",'left')
           ->where($where)
           ->count();
       return ['count' => $count,'data' => $list];
   }
   //临时驾驶员审核
    public function temporarydriveapply($page,$limit,$where){
        $list = db('DriveApply')
            ->alias('da')
            ->field('da.driveapply_id,da.driveapply_old,da.driveapply_card,da.driveapply_name,da.driveapply_tel,da.driveapply_sex,da.driveapply_driveyear,s.store_name,from_unixtime(da.driveapply_auditortime,\'%Y .%m .%d %H:%i:%s\') driveapply_auditortime,a.username,da.driveapply_status')
            ->join('store s',"s.store_id = da.store_id")
            ->join('admin a',"a.admin_id = da.driveapply_auditor","left")
            ->page($page,$limit)
            ->order('da.driveapply_status asc')
            ->where('da.driveapply_type',2)
            ->where($where)
            ->select();
        $count = db('DriveApply')
            ->alias('da')
            ->field('da.driveapply_name,da.driveapply_tel,da.driveapply_sex,da.driveapply_driveyear,s.store_name,from_unixtime(da.driveapply_auditortime,\'%Y .%m .%d %H:%i:%s\') driveapply_auditortime,a.username,da.driveapply_status')
            ->join('store s',"s.store_id = da.store_id")
            ->join('admin a',"a.admin_id = da.driveapply_auditor","left")
            ->where('da.driveapply_type',2)
            ->where($where)
            ->count();
        return ['count' => $count,'data' => $list];
    }
    public function drivething($page,$limit,$where){
        $list = db('drivething')
            ->alias('d')
            ->field("d.drivething_id,from_unixtime(d.drivething_starttime,'%Y .%m .%d %H:%i:%s') as drivething_starttime,from_unixtime(d.drivething_endtime,'%Y .%m .%d %H:%i:%s') as drivething_endtime,d.drivething_type,d.drivething_reason,d.drivething_status,dr.drive_name,dr.drive_tel,a.username,from_unixtime(d.drivething_auditortime,'%Y .%m .%d %H:%i:%s') as drivething_auditortime,s.store_name")
            ->join('drive dr',"d.drive_id = dr.drive_id")
            ->join('store s','dr.store_id = s.store_id','left')
            ->join('admin a','a.admin_id = d.drivething_auditor','left')
            ->where($where)
            ->page($page,$limit)
            ->select();
        $count = db('drivething')
            ->alias('d')
            ->join('drive dr',"d.drive_id = dr.drive_id")
            ->join('store s','dr.store_id = s.store_id','left')
            ->join('admin a','a.admin_id = d.drivething_auditor','left')
            ->where($where)
            ->count();
        return ['count' => $count,'data' => $list];
    }
    public function getPlatformDriverSettlement($where,$page,$limit,$field){
        $list['data'] = db('drive')->alias('d')
            ->field($field)
            ->join('store s','s.store_id = d.store_id')
            ->join('drivingcar_log dg','dg.drive_id = d.drive_id')
            ->join('contractcar c','c.contractcar_id = dg.contractcar_id')
            ->where($where)
            ->page($page,$limit)
            ->group('d.drive_id')
            ->select();
        $list['count'] = db('drive')->alias('d')
            ->field($field)
            ->join('store s','s.store_id = d.store_id')
            ->join('drivingcar_log dg','dg.drive_id = d.drive_id')
            ->join('contractcar c','c.contractcar_id = dg.contractcar_id')
            ->where($where)
            ->count();
        return $list;
    }

}